﻿using System;
using System.Collections.Generic;
using Medianamik.Core.Interfaces;
using Medianamik.Globalization;

namespace Medianamik.Core.Security
{
    public class Group : HierarchicalEntity<Group>, IGroup
    {
        public Group(string name, Guid id, Guid? parentId, string path, int? depth)
            : base(id, parentId, path, depth)
        {
            Name = name;
        }

        protected override Group GetParent(Guid parentId)
        {
            return SecurityManager.GetGroup(parentId);
        }

        public string Name { get; protected set; }

        public int ChildrenCount
        {
            get { return SecurityManager.GetChildGroupsCount(ID); }
        }

        public IEnumerable<Group> GetAllChildGroups()
        {
            return SecurityManager.GetChildGroups(ID, 0);
        }

        public IEnumerable<Group> GetChildGroups(int depth)
        {
            return SecurityManager.GetChildGroups(ID, depth);
        }

        public string TranslatedName
        {
            get
            {
                return String.IsNullOrEmpty(Name) ? "" :
                    Translator.Translate("Medianamik_SecurityManagement", "Group_" + Name);
            }
        }

        public Guid GroupId
        {
            get { return ID; }
        }

        public string GroupName
        {
            get { return Name; }
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as Group);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                {
                    return (base.GetHashCode() * 397) ^ (GroupId.GetHashCode());
                }
            }
        }
    }
}
